package current.security.user.manager.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import current.security.dao.support.BaseHibernateDaoSupport;
import current.security.user.domain.Resc;
import current.security.user.domain.Role;
import current.security.user.domain.U_Role;
import current.security.user.domain.User;


@Repository("userManagerDao")
public class UserManagerDao extends BaseHibernateDaoSupport {
	
	@SuppressWarnings("unchecked")
	public List<Resc> getRescByType(){
		String hql = "from Resc where type = 'URL' order by tree";
		List<Resc> listResc = getHibernateTemplate().find(hql);
		return listResc;
	}
	
	public void addObject(Object o){
		getHibernateTemplate().saveOrUpdate(o);
	}
	
	public void deleteObject(Object o){
		getHibernateTemplate().delete(o);
	}

	public void addUser(User user){
		getHibernateTemplate().save(user);
	}
	
	public void updateUser(User user){
		getHibernateTemplate().update(user);
	}
	
	@SuppressWarnings("unchecked")
	public User getUserByName(String userName){
		String hql = "from User where name = ?";
		List<User> user =  (List<User>)getHibernateTemplate().find(hql,userName);
		if(user != null && user.size() > 0){
			User dbuser = user.get(0);
			return dbuser;
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public Integer delete(final String hql,final String id){
		return getHibernateTemplate().execute(new HibernateCallback(){
			@Override
			public Integer doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				query.setString(0, id);
				return query.executeUpdate();
			}
			
		});
	}
	
	public Object getObjectById(Class<?> c,String id){
		String hql = "from "+c.getSimpleName()+" where id = ?";
		List<?> list = getHibernateTemplate().find(hql, id);
		if(list!= null && list.size() > 0){
			return list.get(0);
		}
		return null;
	}
	
	
	public Resc getRescById(Integer id){
		String hql = "from Resc where id = ?";
		List<?> listResc = getHibernateTemplate().find(hql, id);
		System.out.println(listResc);
		if(listResc!= null && listResc.size() > 0){
			return (Resc)listResc.get(0);
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<User> getAllUser(){
		String hql = "from User order by id";
		List<User> listUser =  getHibernateTemplate().find(hql);
		return listUser;
	}
	
	@SuppressWarnings("unchecked")
	public List<User> getAllActiveUser(){
		String hql = "from User where status = 1 order by id";
		List<User> listUser =  getHibernateTemplate().find(hql);
		return listUser;
	}
	
	@SuppressWarnings("unchecked")
	public List<User> getUserByRoleName(String roleName){
		if (StringUtils.isBlank(roleName)) return null;
		String cs = "";
		if(roleName.indexOf(",") != -1){
			String roleNameArray [] = roleName.split(",");
			for (int i = 0; i < roleNameArray.length; i++) {
				cs = cs + " role.name = '"+ roleNameArray[i] + "' or ";
			}
			cs = cs + " '1' = '2' ";
		} else {
			cs = " role.name = '"+roleName+"' ";
		}
		List<User> listUser = new ArrayList<User>();
		try{
			String hql = "from U_Role where "+cs+" and user.status = 1 order by id";
			List<U_Role> list = getHibernateTemplate().find(hql);
			if(list != null){
				for (Iterator iterator = list.iterator(); iterator.hasNext();) {
					U_Role uRole = (U_Role) iterator.next();
					listUser.add(uRole.getUser());
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return listUser;
	}
	
	@SuppressWarnings("unchecked")
	public List<User> getUserByRoleNameAndCompany(String roleName,String companyId){
		if (StringUtils.isBlank(roleName)) return null;
		String cs = "";
		if(roleName.indexOf(",") != -1){
			String roleNameArray [] = roleName.split(",");
			for (int i = 0; i < roleNameArray.length; i++) {
				cs = cs + " role.name = '"+ roleNameArray[i] + "' or ";
			}
			cs = cs + " '1' = '2' ";
		} else {
			cs = " role.name = '"+roleName+"' ";
		}
		if (StringUtils.isBlank(roleName)) return null;
		List<User> listUser = new ArrayList<User>();
		String hql = "from U_Role where user.department.companyId = ? and ("+cs+") and user.status = 1 order by id";
		List<U_Role> list = getHibernateTemplate().find(hql,companyId);
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			U_Role uRole = (U_Role) iterator.next();
			listUser.add(uRole.getUser());
		}
		return listUser;
	}
	
	@SuppressWarnings("unchecked")
	public List<User> getUserByRoleNameAndDepartment(String roleName,String departmentId){
		if (StringUtils.isBlank(roleName)) return null;
		String cs = "";
		if(roleName.indexOf(",") != -1){
			String roleNameArray [] = roleName.split(",");
			for (int i = 0; i < roleNameArray.length; i++) {
				cs = cs + " role.name = '"+ roleNameArray[i] + "' or ";
			}
			cs = cs + " '1' = '2' ";
		} else {
			cs = " role.name = '"+roleName+"' ";
		}
		if (StringUtils.isBlank(roleName)) return null;
		List<User> listUser = new ArrayList<User>();
		String hql = "from U_Role where user.department.id = ? and ("+cs+") and user.status = 1 order by id";
		List<U_Role> list = getHibernateTemplate().find(hql,departmentId);
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			U_Role uRole = (U_Role) iterator.next();
			listUser.add(uRole.getUser());
		}
		return listUser;
	}
	
	@SuppressWarnings("unchecked")
	public List<User> getUserByDepartment(String departmentId){
		if (StringUtils.isBlank(departmentId)) return null;
		List<User> listUser = new ArrayList<User>();
		String hql = "from U_Role where user.department.id = ? and user.status = 1 and user.name != 'admin' order by id";
		List<U_Role> list = getHibernateTemplate().find(hql,departmentId);
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			U_Role uRole = (U_Role) iterator.next();
			listUser.add(uRole.getUser());
		}
		return listUser;
	}
	
	
	@SuppressWarnings("unchecked")
	public List<Role> getAllRole(){
		String hql = "from Role order by id";
		List<Role> listUser =  getHibernateTemplate().find(hql);
		return listUser;
	}
	
	@SuppressWarnings("unchecked")
	public List<Resc> getAllResc(){
		String hql = "from Resc order by tree";
		List<Resc> listResc =  getHibernateTemplate().find(hql);
		return listResc;
	}
	
	public HibernateTemplate getHbTemplate() {
		return getHibernateTemplate();
	}

}
